<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * This object represent the followin xml:
 *   <subscription>
 *     <name>Hei Hoi</name>
 *     <uri>http://callback.com/callback.php</uri>
 *     <atomfeed>http://identi.ca/api/statuses/user_timeline/209172.atom</atomfeed>
 *     <alternate>http://identi.ca/api/statuses/user_timeline/209172.atom</alternate>
 *     <logo>SECRETQSDF345SDRT35ERG35SDFG35SRG</logo>
 *   </subscription>
 * 
 * TODO! Add the salmon endpoint here. And the public key too I think.
 * 
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Minime_User_Subscriber {
    
    private $uri;
    private $name;
    private $atomfeed;
    private $alternate;
    private $logo;
    
    const ELEMENT_SUBSCRIBER = "subscriber";
    
    private function __construct() {
        
    }
    
    private function setName($name) {
        if(empty($name)) {
            throw new Exception("Name cannot be set empty.");
        }
        //$this->name = htmlentities($name, ENT_QUOTES, "UTF-8", false);
        $this->name = htmlspecialchars($name, ENT_QUOTES, "UTF-8", false);
    }
    
    public function getName() {
        return $this->name;
    }
    
    private function setUri($uri) {
        if(empty($uri)) {
            throw new Exception("Uri cannot be set empty.");
        }
        //$this->uri = htmlentities($uri, ENT_QUOTES, "UTF-8", false);
        $this->uri = htmlspecialchars($uri, ENT_QUOTES, "UTF-8", false);
    }
    
    public function getUri() {
        return $this->uri;
    }
    
    private function setAtomfeede($atomfeed) {
        if(empty($atomfeed)) {
            throw new Exception("Atomfeed cannot be set empty.");
        }
        //$this->atomfeed = htmlentities($atomfeed, ENT_QUOTES, "UTF-8", false);
        $this->atomfeed = htmlspecialchars($atomfeed, ENT_QUOTES, "UTF-8", false);
    }
    
    private function setAlternate($alternate) {
        if(empty($alternate)) {
            throw new Exception("Alternate cannot be set empty.");
        }
        //$this->alternate = htmlentities($alternate, ENT_QUOTES, "UTF-8", false);
        $this->alternate = htmlspecialchars($alternate, ENT_QUOTES, "UTF-8", false);
    }

    public function getAlternate() {
        return $this->alternate;
    }
    
    private function setLogo($logo) {
//        if(empty($logo)) {
//            throw new Exception("Logo cannot be set empty.");
//        }
        //$this->logo = htmlentities($logo, ENT_QUOTES, "UTF-8", false);
        $this->logo = htmlspecialchars($logo, ENT_QUOTES, "UTF-8", false);
    }
    
    public function getLogo() {
        return $this->logo;
    }
    
    public static function buildFromDOMElement(DOMElement $elm) {
        
        $sub = new Minime_User_Subscriber();
        $sub->setName($elm->getElementsByTagName("name")->item(0)->nodeValue);
        $sub->setUri($elm->getElementsByTagName("uri")->item(0)->nodeValue);
        $sub->setAtomfeede($elm->getElementsByTagName("atomfeed")->item(0)->nodeValue);
        $sub->setAlternate($elm->getElementsByTagName("alternate")->item(0)->nodeValue);
        $sub->setLogo($elm->getElementsByTagName("logo")->item(0)->nodeValue);
        return $sub;
    }
    
    public function asDomElement(DOMDocument $dom) {
        $element = $dom->createElement(self::ELEMENT_SUBSCRIBER);
        
        $name = $dom->createElement('name', $this->name);
        $uri = $dom->createElement('uri', $this->uri);
        $atomfeed = $dom->createElement('atomfeed', $this->atomfeed);
        $alternate = $dom->createElement('alternate', $this->alternate);
        $logo = $dom->createElement('logo', $this->logo);
        
        $element->appendChild($name);
        $element->appendChild($uri);
        $element->appendChild($atomfeed);
        $element->appendChild($alternate);
        $element->appendChild($logo);
        
        return $element;
    }
    
    public static function buildFromArray(array $array) {
        $sub = new Minime_User_Subscriber();
        $sub->setUri($array['uri']);
        $sub->setName($array['name']);
        $sub->setAtomfeede($array['atomfeed']);
        $sub->setAlternate($array['alternate']);
        $sub->setLogo($array['logo']);
        return $sub;
    }
}